﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace NewFormatProject.Infrastructure.Tools
{
    public static class Tools
    {
        public static string Guid_1()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }
        public static string Guid_2()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Guid.NewGuid().ToString().Replace("-", "").Substring(10);
        }
        public static string GetMainProgramPath()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..");
        }
        public static string GetUploadImagePath()
        {
            return Path.Combine(GetMainProgramPath(), "wwwroot", "Uploads", "Images");
        }
        public static void DeleteImage(string path)
        {
            // 检查文件是否存在
            if (System.IO.File.Exists(path))
            {
                // 删除文件
                System.IO.File.Delete(path);
            }
        }
        private const long _maxImageFileSize = 2 * 1024 * 1024; // 2MB
        private static readonly List<string> _allowedImageExtensions = new List<string> { ".jpg", ".jpeg", ".png", ".gif" };
        private static readonly List<string> _allowedImageContentTypes = new List<string> { "image/jpeg", "image/png", "image/gif" };
        private static bool IsFileSizeValid(IFormFile file, long maxSize)//2 * 1024 * 1024 2mb
        {
            return file.Length <= maxSize;
        }
        private static bool IsFileTypeValid(IFormFile file, List<string> allowedExtensions, List<string> allowedContentTypes)
        {
            var extension = Path.GetExtension(file.FileName).ToLowerInvariant();
            var contentType = file.ContentType.ToLowerInvariant();
            return allowedExtensions.Contains(extension) && allowedContentTypes.Contains(contentType);
        }
        public static async Task<string> UploadImage(IFormFile imageFile, string savePath)
        {
            if (!IsFileSizeValid(imageFile, _maxImageFileSize))
            {
                return null;
            }
            if (!IsFileTypeValid(imageFile, _allowedImageExtensions, _allowedImageContentTypes))
            {
                return null;
            }
            return await Upload(imageFile, savePath);
        }
        private static async Task<string> Upload(IFormFile file, string pathDir)
        {
            var fileName = Guid_2() + Path.GetExtension(file.FileName).ToLowerInvariant();

            // 确保uploads文件夹存在
            if (!Directory.Exists(Path.GetDirectoryName(pathDir)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(pathDir));
            }

            using (var stream = new FileStream(Path.Combine(pathDir,fileName), FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return fileName;
        }
    }
}
